﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WLW_Electricity_detection
{
    public partial class Form_阈值配置 : Form
    {
        public Form_阈值配置()
        {
            InitializeComponent();

            textBox_缺电_Q.Text = Share.power_shortage_Q.ToString();
            textBox_缺电.Text = Share.power_shortage.ToString();
            textBox_低能量_Q.Text = Share.power_shortage.ToString();
            textBox_低能量.Text = Share.low_energy.ToString();
            textBox_正常状态_Q.Text = Share.low_energy.ToString();
            textBox_正常状态.Text = Share.order.ToString();
            textBox_满载范围_Q.Text = Share.order.ToString();
            textBox_满载范围.Text = Share.fully_loaded.ToString();
        }

        void Enter_judgment(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 0x20) e.KeyChar = (char)0;  //禁止空格键
            if ((e.KeyChar == 0x2D) && (((TextBox) sender).Text.Length == 0)) return;   //处理负数
            if (e.KeyChar > 0x20)
            {
                try
                {
                    double.Parse(((TextBox) sender).Text + e.KeyChar.ToString());
                }
                catch
                {
                    e.KeyChar = (char)0;   //处理非法字符
                }
            }
        }

        private void Form_阈值配置_Load(object sender, EventArgs e)
        {
            textBox_缺电_Q.Text = Share.power_shortage_Q.ToString();
            textBox_缺电.Text = Share.power_shortage.ToString();
            textBox_低能量_Q.Text = Share.power_shortage.ToString();
            textBox_低能量.Text = Share.low_energy.ToString();
            textBox_正常状态_Q.Text = Share.low_energy.ToString();
            textBox_正常状态.Text = Share.order.ToString();
            textBox_满载范围_Q.Text = Share.order.ToString();
            textBox_满载范围.Text = Share.fully_loaded.ToString();
        }

        private void button_更新_Click(object sender, EventArgs e)
        {
            if (textBox_缺电_Q.Text != "" && textBox_缺电.Text != ""
                && textBox_低能量_Q.Text != "" && textBox_低能量.Text != ""
                && textBox_正常状态_Q.Text != "" && textBox_正常状态.Text != ""
                && textBox_满载范围_Q.Text != "" && textBox_满载范围.Text != "")
            {
                string Excessive_value = "";

                if (float.Parse(textBox_缺电_Q.Text) > float.Parse(textBox_缺电.Text))
                {
                    Excessive_value = textBox_缺电_Q.Text;
                    textBox_缺电_Q.Text = textBox_缺电.Text;
                    textBox_缺电.Text = Excessive_value;
                }
                if (float.Parse(textBox_低能量_Q.Text) > float.Parse(textBox_低能量.Text))
                {
                    Excessive_value = textBox_低能量_Q.Text;
                    textBox_低能量_Q.Text = textBox_低能量.Text;
                    textBox_低能量.Text = Excessive_value;

                    if (textBox_缺电.Text != textBox_低能量_Q.Text)
                        textBox_低能量_Q.Text = textBox_缺电.Text;
                }
                if (float.Parse(textBox_正常状态_Q.Text) > float.Parse(textBox_正常状态.Text))
                {
                    Excessive_value = textBox_正常状态_Q.Text;
                    textBox_正常状态_Q.Text = textBox_正常状态.Text;
                    textBox_正常状态.Text = Excessive_value;

                    if (textBox_正常状态_Q.Text != textBox_低能量.Text)
                        textBox_正常状态_Q.Text = textBox_低能量.Text;
                }
                if (float.Parse(textBox_满载范围_Q.Text) > float.Parse(textBox_满载范围.Text))
                {
                    Excessive_value = textBox_满载范围_Q.Text;
                    textBox_满载范围_Q.Text = textBox_满载范围.Text;
                    textBox_满载范围.Text = Excessive_value;

                    if (textBox_满载范围_Q.Text != textBox_正常状态.Text)
                        textBox_满载范围_Q.Text = textBox_正常状态.Text;
                }

                Share.power_shortage_Q = double.Parse(textBox_缺电_Q.Text);
                Share.power_shortage = double.Parse(textBox_缺电.Text);
                Share.low_energy = double.Parse(textBox_低能量.Text);
                Share.order = double.Parse(textBox_正常状态.Text);
                Share.fully_loaded = double.Parse(textBox_满载范围.Text);
                this.Hide();
            }
        }

        private void textBox_缺电_TextChanged(object sender, EventArgs e)
        {
            textBox_低能量_Q.Text = textBox_缺电.Text;
        }

        private void textBox_低能量_Q_TextChanged(object sender, EventArgs e)
        {
            textBox_缺电.Text = textBox_低能量_Q.Text;
        }

        private void textBox_低能量_TextChanged(object sender, EventArgs e)
        {
            textBox_正常状态_Q.Text = textBox_低能量.Text;
        }

        private void textBox_正常状态_Q_TextChanged(object sender, EventArgs e)
        {
            textBox_低能量.Text = textBox_正常状态_Q.Text;
        }

        private void textBox_正常状态_TextChanged(object sender, EventArgs e)
        {
            textBox_满载范围_Q.Text = textBox_正常状态.Text;
        }

        private void textBox_满载范围_Q_TextChanged(object sender, EventArgs e)
        {
            textBox_正常状态.Text = textBox_满载范围_Q.Text;
        }

        private void textBox_缺电_Q_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox_缺电_Q_KeyPress(object sender, KeyPressEventArgs e)
        {
            Enter_judgment(sender, e);
        }

        private void textBox_缺电_KeyPress(object sender, KeyPressEventArgs e)
        {
            Enter_judgment(sender, e);
        }

        private void textBox_低能量_Q_KeyPress(object sender, KeyPressEventArgs e)
        {
            Enter_judgment(sender, e);
        }

        private void textBox_低能量_KeyPress(object sender, KeyPressEventArgs e)
        {
            Enter_judgment(sender, e);
        }

        private void textBox_正常状态_Q_KeyPress(object sender, KeyPressEventArgs e)
        {
            Enter_judgment(sender, e);
        }

        private void textBox_正常状态_KeyPress(object sender, KeyPressEventArgs e)
        {
            Enter_judgment(sender, e);
        }

        private void textBox_满载范围_Q_KeyPress(object sender, KeyPressEventArgs e)
        {
            Enter_judgment(sender, e);
        }

        private void textBox_满载范围_KeyPress(object sender, KeyPressEventArgs e)
        {
            Enter_judgment(sender, e);
        }
    }
}
